/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProgressBarFrame.java
 *
 * Created on 06/11/2010, 14:10:09
 */
package user_gui;

import core.MyWorker;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 *
 * @author Jônatas
 */
public class ProgressBarFrame extends javax.swing.JFrame implements PropertyChangeListener, Runnable {

    private JFrame parent;
    private MyWorker task;
    private boolean finished;

    public ProgressBarFrame(MyWorker task, JFrame parent, String title, String message) {
        initComponents();
        this.task = task;
        this.parent = parent;
        setTitle(title);
        jLabel1.setText(message);
        parent.setEnabled(false);
        progressBar.setIndeterminate(false);
        finished = false;
    }

    public boolean isFinished() {
        return finished;
    }

    public void execute() {
        task.addPropertyChangeListener(this);
        task.execute();
    }

    public SwingWorker getTask() {
        return task;
    }

    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {

            String message = task.getMessage();
            if(message != null) {
                jLabel1.setText(message);
            }
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
            if (progress >= 100) {
                this.close();
            }
        }
    }

    private void close() {
        finished = true;
        this.parent.setEnabled(true);
        this.dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressBar = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Carregando");
        setResizable(false);

        progressBar.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        progressBar.setStringPainted(true);

        jLabel1.setText("Fazendo atividade");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                    .addComponent(progressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-304)/2, (screenSize.height-106)/2, 304, 106);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public JProgressBar getjProgressBar1() {
        return progressBar;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar progressBar;
    // End of variables declaration//GEN-END:variables

    public void run() {
        setVisible(true);
        execute();
    }
}
